import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class TeacherFile {
    public TeacherFile() {
        File file = new File("teacher.txt");
        if(!file.exists())
        {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void printself(String user){
        ArrayList arrayList = getArrayList();
        for(int i=0;i<arrayList.size();i++)
        {
            Teacher teacher = new Teacher();
            teacher = (Teacher) arrayList.get(i);
            if(teacher.getUser().equals(user))
            {
                System.out.println(teacher.getName()+"\t"+teacher.getUser()+"\t"+teacher.getPwd());
            }
        }
    }
    public void input()
    {
        Scanner in = new Scanner(System.in);
        String id;
        String pwd;
        String name;
        String cell;
        String dorm;
        String cla;
        int score;
        StudentFile studentFile = new StudentFile();
        ArrayList arrayList = studentFile.getArrayList();
        Student student = new Student();
        System.out.println("请输入学生id");
        id = in.nextLine();
        System.out.println("请输入学生密码");
        pwd = in.nextLine();
        System.out.println("请输入学生姓名");
        name = in.nextLine();
        System.out.println("请输入学生电话");
        cell = in.nextLine();
        System.out.println("请输入学生宿舍");
        dorm = in.nextLine();
        System.out.println("请输入学生班级");
        cla = in.nextLine();
        System.out.println("请输入学生分数");
        score= in.nextInt();
        student.setId(id);
        student.setPwd(pwd);
        student.setName(name);
        student.setCell(cell);
        student.setDorm(dorm);
        student.setCla(cla);
        student.setScore(score);
        arrayList.add(student);
        studentFile.SaveStudentFile(arrayList);
    }
    public void findone()
    {
        Scanner in = new Scanner(System.in);
        StudentFile studentFile = new StudentFile();
        ArrayList arrayList =studentFile.getArrayList();
        Student student = new Student();
        System.out.println("请输入需查找的学生姓名");
        String name = in.nextLine();
        for(int i=0;i<arrayList.size();i++)
        {
            student = (Student) arrayList.get(i);
            if(student.getName().equals(name))
            {
                System.out.println(student.getName()+"\t"+student.getId()+"\t"+student.getPwd()+"\t"+student.getCla()+"\t"+student.getCell()+"\t"+student.getDorm()+"\t"+student.getScore());
            }
        }
    }
    public void updateStudent()
    {
        String cell;
        String dorm;
        String cla;
        int score;
        StudentFile studentFile =new StudentFile();
        ArrayList arrayList = studentFile.getArrayList();
        Scanner in = new Scanner(System.in);
        Student student = new Student();
        System.out.println("请输入你需要更改的学生name");
        String name = in.nextLine();
        for(int i=0;i<arrayList.size();i++)
        {
            student= (Student) arrayList.get(i);
            if(student.getName().equals(name))
            {
                System.out.println("请输入修改的电话");
                cell = in.nextLine();
                System.out.println("请输入修改的宿舍号");
                dorm = in.nextLine();
                System.out.println("请输入修改的班级");
                cla = in.nextLine();
                System.out.println("请输入修改的分数");
                score = in.nextInt();
                student.setCell(cell);
                student.setDorm(dorm);
                student.setCla(cla);
                student.setScore(score);
                studentFile.SaveStudentFile(arrayList);
            }
            else
                System.out.println("找不着人");
        }
    }
    public void updatePwd(String user)
    {
        Scanner in = new Scanner(System.in);
        ArrayList arrayList = getArrayList();
        System.out.print("请更新密码:");
        String pwd = in.nextLine();
        for(int i=0;i<arrayList.size();i++)
        {
            Teacher teacher = new Teacher();
            teacher = (Teacher) arrayList.get(i);
            if(teacher.getUser().equals(user))
            {
                teacher.setPwd(pwd);
            }
        }
        SaveTeacherFile(arrayList);
    }
    public void printallStudent(){
        StudentFile studentFile = new StudentFile();
        studentFile.printall();
    }
    public void SaveTeacherFile(ArrayList arrayList){
        try {
            FileWriter fw = new FileWriter("teacher.txt");
            for(int i=0;i<arrayList.size();i++)
            {
                Teacher teacher = new Teacher();
                teacher = (Teacher) arrayList.get(i);
                fw.append(teacher.getName()+"\t"+teacher.getUser()+"\t"+teacher.getPwd());
                fw.append("\n");
            }
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public ArrayList getArrayList(){
        ArrayList arrayList = new ArrayList();
        try {
            FileReader fr = new FileReader("teacher.txt");
            BufferedReader br = new BufferedReader(fr);
            String buf = null;
            while((buf=br.readLine())!=null)
            {
                Teacher teacher = new Teacher();
                String[] strings;
                strings = buf.split("\t");
                teacher.setName(strings[0]);
                teacher.setUser(strings[1]);
                teacher.setPwd(strings[2]);
                arrayList.add(teacher);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return arrayList;
    }
}
